const express        = require('express');
const router = express.Router();

var xlsxj = require("xlsx-to-json");
var multer  = require('multer')
var cors = require('cors');

function parse(req, res, next){
    var file = req.file;
    xlsxj({
            input: req.file.path, 
            output: `output/util/xlsx/dump/${new Date().getTime()}_${req.file.filename}.json`
            // output: `/home/wwwroot/www.talenmall.com/mail/backend/output/util/xlsx/dump/${new Date().getTime()}_${req.file.filename}.json`
            }, 
            function(err, result) {
                if(err) {
                    res.send({code:1, err:err, message: "xlsx文件解析错误"});
                }else {
                    req.xlsx2json = result;
                    next();
                }
        });
}

router.use(cors())
var uploadXlsx = multer({ dest: './uploads/util/xlsx/' })
router.post('/', uploadXlsx.single('xlsx'), parse, (req, res, next)=>{
    res.send({code: 0, result: req.xlsx2json});
});

module.exports = router;